﻿package net.wangit.framework.util;

import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import net.wangit.context.LocalContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Response输入类，例如用于输出错误信息
 * 
 * @author watson
 * Feb 14, 2010
 */
public class ResponseWriter {

	private static final Log log = LogFactory
			.getLog(net.wangit.framework.util.ResponseWriter.class);

	public ResponseWriter() {
	}

	public static void writeWithHtml(String html) {
		write((new StringBuilder("<html><p>")).append(html).append(
				"</p></html>").toString());
	}

	public static void write(String html) {
		LocalContext lc = LocalContext.getLocalContext();
		HttpServletResponse response = lc.getHttpServletResponse();
		write(response, html);
	}

	public static void write(HttpServletResponse response, String html) {
		try {
			OutputStream out = ResponseUtil.setting(response);
			out.write(html.getBytes(response.getCharacterEncoding()));
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

}
